//
//  XCWelcomeController.swift
//  XCMicroBo
//
//  Created by 1 on 16/3/10.
//  Copyright © 2016年 1. All rights reserved.
//

import UIKit
import SnapKit
import SDWebImage


class XCWelcomeController: UIViewController {

    override func loadView() {
        view = backImageView
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
        
        setUI()
    }

    func setUI(){
    
        view.addSubview(iconImageView)
        view.addSubview(welcomLabel)
        
        //设置尺寸
        iconImageView.snp_makeConstraints { (make) -> Void in
            
            make.centerX.equalTo(view.snp_centerX)
            make.bottom.equalTo(view.snp_bottom).offset(-180)
            
            //设置宽高
            make.width.equalTo(90)
            make.height.equalTo(90)
        }
        
        welcomLabel.snp_makeConstraints { (make) -> Void in
            
            make.top.equalTo(iconImageView.snp_bottom).offset(18)
            make.centerX.equalTo(iconImageView.snp_centerX)
        }
        
        iconImageView.sd_setImageWithURL(NSURL(string: UserCount.shareAcount.avatar_large ?? ""))


    }
    
    override func viewDidAppear(animated: Bool) {
        super.viewDidAppear(animated)
        
        //动画要在这个方法中开启
        //这时候子控件的frame已经完全确定了   这时候在设置frame就没什么问题
        startAnimation()
    }
    
    //开始动画
    func startAnimation(){
        
        let offset = -UIScreen.mainScreen().bounds.height + 180
        
//        iconImageView.snp_makeConstraints { (make) -> Void in
//            
//            make.top.equalTo(view.snp_top).offset(offset)
//        }
        
        iconImageView.snp_updateConstraints { (make) -> Void in
            make.bottom.equalTo(view.snp_bottom).offset(offset)

        }
        
        
        
        UIView.animateWithDuration(1.5, delay:0, usingSpringWithDamping: 0.98, initialSpringVelocity: 9.8, options: [], animations: { () -> Void in
            
            self.view.layoutIfNeeded()
            self.welcomLabel.alpha = 1

            }) { (_) -> Void in
                
                //登录完成时  跳转界面 
                //不建议使用下面这种方法来跳转控制器  因为如果要跳转的控制器数量少还可以接受  如果多的话  分散在各个位置   相当不便于后期的维护  
                //建议用通知来完成控制器的跳转
//                UIApplication.sharedApplication().keyWindow?.rootViewController = XCTabBarController()
                
                
                //发送通知
                NSNotificationCenter.defaultCenter().postNotificationName(swiftController ,  object: nil)
                
            }
        

    }
    
    
    
    //懒加载背景视图
    lazy var backImageView = UIImageView(image: UIImage(named: "ad_background"))
    
    //懒加载头像视图
    lazy var iconImageView = UIImageView(image: UIImage(named:"avatar_default_big"))
    

    //懒加载标签视图
    lazy var welcomLabel:UILabel = {
        
        let label = UILabel()
        
        label.text = ((UserCount.shareAcount.name) ?? "") + "  欢迎回来"
        
        label.textColor = UIColor.grayColor()

        label.textAlignment = .Center
        
        label.font = UIFont.systemFontOfSize(16)
        
        label.alpha = 0
        
        label.sizeToFit()
        return label
    }()

}
